<?php

namespace SunAdmin\Controller;

use SunAdmin\Service\Ws\WsHandle;
use Swoole\WebSocket\Frame;
use think\swoole\Websocket;

class WsController
{
    /**
     * 后台管理系统WebSocket连接
     */
    public function sunAdmin()
    {
        $wsHandle = new WsHandle('admin');
        
        return \think\swoole\helper\websocket()
            ->onOpen(function (Websocket $websocket, $request) use ($wsHandle) {
                $wsHandle->onOpen($websocket, $request);
            })
            ->onMessage(function (Websocket $websocket, Frame $frame) use ($wsHandle) {
                $wsHandle->onMessage($websocket, $frame);
            })
            ->onClose(function (Websocket $websocket) use ($wsHandle) {
                $wsHandle->onClose($websocket);
            });
    }

    /**
     * 客户端WebSocket连接
     */
    public function client()
    {
        $wsHandle = new WsHandle('client');
        
        return \think\swoole\helper\websocket()
            ->onOpen(function (Websocket $websocket, $request) use ($wsHandle) {
                $wsHandle->onOpen($websocket, $request);
            })
            ->onMessage(function (Websocket $websocket, Frame $frame) use ($wsHandle) {
                $wsHandle->onMessage($websocket, $frame);
            })
            ->onClose(function (Websocket $websocket) use ($wsHandle) {
                $wsHandle->onClose($websocket);
            });
    }
}